#define		_TBOX_GSM_
#include "config.h"



#define			GSM_RESET_ON	IO_Write(IO_GSM_RST,TRUE)
#define			GSM_RESET_OFF	IO_Write(IO_GSM_RST,FALSE)

#define			GSM_PWR_ON				
#define			GSM_PWR_OFF				



#define		AT_AT		"AT\r\n"
#define		AT_ECHO		"ATE0\r\n"
#define		AT_IMEI		"AT+CGSN=1\r\n"
#define		AT_CCID		"AT+QCCID\r\n"
#define		AT_STATE	"AT+QISTATE\r\n"
#define		AT_QIRD		"AT+QISTATE=?\r\n"
#define		AT_CLOSE	"AT+QICLOSE=2,1000\r\n"
#define		AT_CIMI		"AT+CIMI\r\n"
#define		AT_QFLDS	"AT+QFLDS=\"UFS\"\r\n"
#define		AT_QFLST	"AT+QFLST=\"SFS:*\"\r\n"
#define		AT_QCAFILE	"AT+QFLST=\"SFS:CA.crt\"\r\n"
#define		AT_QCLIENT	"AT+QFLST=\"SFS:Client.crt\"\r\n"
#define		AT_QKEY		"AT+QFLST=\"SFS:Client.key\"\r\n"
#define		AT_QFDEL	"AT+QFDEL=\"SFS:*\"\r\n"
#define		AT_GMR		"AT+GMR\r\n"
#define		AT_QSCLK    "AT+QSCLK=1\r\n"
#define		AT_CMGF    	"AT+CMGF=1\r\n"
#define		AT_CSCS    	"AT+CSCS=\"GSM\"\r\n"
#define		AT_CNMI   	"AT+CNMI=1,2,0,1,0\r\n"
#define		AT_QURCCFG	"AT+QURCCFG=\"urcport\",\"uart1\"\r\n"
#define		AT_CREG0	"AT+CEREG=1\r\n"
#define		AT_CREG1	"AT+CEREG?\r\n"

#define		AT_CFUN0    "AT+CFUN=0\r\n"
#define		AT_CFUN1    "AT+CFUN=1\r\n"

#define		AT_NCCID	"AT+NCCID\r\n"
#define		AT_CGATT0	"AT+CGATT=1\r\n"
#define		AT_CGATT1	"AT+CGATT?\r\n"
#define		AT_NCDP		"AT+NCDP=180.101.147.115,5683\r\n"
#define		AT_NBAND	"AT+NBAND=5\r\n"
#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"CTNB\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psm0.eDRX0.ctnb\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psmA.eDRX0.ctnb\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psmC.eDRX0.ctnb\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psmF.eDRXC.ctnb\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psm0.eDRXH.ctnb\"\r\n"
//#define		AT_CGDCONT0	"AT+CGDCONT=0,\"IP\",\"psm0.eDRXC.ctnb\"\r\n"


#define		AT_CGDCONT1	"AT+CGDCONT?\r\n" //
#define		AT_NMSG		"AT+NMGS=1,aa\r\n"
#define		AT_CGPADDR	"AT+CGPADDR\r\n"
#define		AT_NNMI		"AT+NNMI=1\r\n"	
#define		AT_NSMI		"AT+NSMI=1\r\n"	
#define		AT_CSQ		"AT+CSQ\r\n"	//NUESTATS
#define		AT_NCONFIG	"AT+NCONFIG?\r\n"
#define		AT_CSCON	"AT+CSCON?\r\n"
#define		AT_CCLK		"AT+CCLK?\r\n"

#define		AT_TEST		""
char AT_CMD[512] = {'\0'};
UINT16 atcmdLength = 0;
T_GSM_TX_INFO tempTxInfo;
/********************************************************************************************************************************************************************************************
**@function:		str2int	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
static UINT32 str2int(char *pSrc,BYTE len)
{
	UINT32 value = 0;
	BYTE i = 0;
	for(i = 0;i < len;i++)
	{
		value *= 10;
		value += pSrc[i] - '0';
	}
	
	return value;
}
/********************************************************************************************************************************************************************************************
**@function:		gsmSetTimestamp	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmSetTimestamp(void)
{
	UINT16 i = 0,len = 0,j = 0;
	struct tm stm;
	UINT32 timestamp = 0;
	UINT16 year = 0;
	BYTE month = 0,day = 0,hour = 0,min = 0,sec = 0;
    memset(&stm,0,sizeof(stm));
	BYTE data = 0;
	char chtime[21] = {'\0'};
	len = FIFO_GET_COUNT(GSMRxFifo);
	if(len > 20)
	{
		len = 20;
	}
	
	printf("\r\n len: %d: ",len);
	for(i = 0;i < len;i++)
	{
		FIFO_GET_ONE(GSMRxFifo,&data);
		printf("%c",data);
		if(('0' <= data) && ('9' >= data))
		{
			chtime[j++] = data;
		}
		
	}
	
	printf("\r\n gsm time: %s",chtime);	
    year = str2int(&chtime[0],2) + 2000;
	month = str2int(&chtime[2],2);
	day = str2int(&chtime[4],2);
	hour = str2int(&chtime[6],2);
	min = str2int(&chtime[8],2);
	sec = str2int(&chtime[10],2);
	stm.tm_year=year-1900;
    stm.tm_mon=month-1;
    stm.tm_mday=day;
    stm.tm_hour=hour;
    stm.tm_min=min;
    stm.tm_sec=sec;
    timestamp = mktime(&stm);
	printf("\r\n timestamp: %u",timestamp);
	setTimeStamp(timestamp);
}
/********************************************************************************************************************************************************************************************
**@function:		hex2str	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
static UINT16 hex2str(BYTE *pSrc,UINT16 len,char *pDes)
{
	UINT16 i = 0;
	for(i = 0;i < len;i++)
	{
		sprintf(&pDes[i * 2],"%02X",pSrc[i]);
	}
	return len * 2;
}
/********************************************************************************************************************************************************************************************
**@function:		str2hex	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
static void str2hex(char *pSrc,BYTE len,BYTE *pHex)
{
	BYTE temp = 0;
	*pHex = 0;
	if((pSrc[0] >= '0') && (pSrc[0] <= '9'))
	{
		*pHex = pSrc[0] - '0';
	}
	else if((pSrc[0] >= 'a') && (pSrc[0] <= 'f'))
	{
		*pHex = (pSrc[0] - 'a') + 10;
	}
	else if((pSrc[0] >= 'A') && (pSrc[0] <= 'F'))
	{
		*pHex = (pSrc[0] - 'A') + 10;
	}
	
	*pHex <<=4;
	
	if((pSrc[1] >= '0') && (pSrc[1] <= '9'))
	{
		*pHex |= pSrc[1] - '0';
	}
	else if((pSrc[1] >= 'a') && (pSrc[1] <= 'f'))
	{
		*pHex |= (pSrc[1] - 'a') + 10;
	}
	else if((pSrc[1] >= 'A') && (pSrc[1] <= 'F'))
	{
		*pHex |= (pSrc[1] - 'A') + 10;
	}
	
}
/********************************************************************************************************************************************************************************************
**@function:		printATCmd	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void printATCmd(BOOL bHex,char *p,UINT16 len)
{
	UINT16 i = 0;
	printf("\r\n print ATCMD: ");
	for(i = 0;i < len;i++)
	{
		if(bHex) //
		{
			printf("%02x ",p[i]);
		}
		else
		{
			printf("%c",p[i]);
		}
	}
	
	for(i = 0;i < len;i++)
	{
		PrintUartMsg(USART2,p[i]);
	}
	
	gsmTaskInfo.atCmdStatus = 0x00;	 
}
/********************************************************************************************************************************************************************************************
**@function:		printFrameToGSM	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void printFrameToGSM(BYTE *p,UINT16 len)
{
	UINT16 i = 0;
	memset(&tempTxInfo,0,sizeof(tempTxInfo));
	if(len > 512)
	{
		printf("\r\n printFrameToGSM len is to long...");
		len = 512;
	}
	
	tempTxInfo.frameLength = len;
	for(i = 0;i < len;i++)
	{
		tempTxInfo.frameBuf[i] = p[i];
	}
	FIFO_PUT_ONE(GSMTxFifo,tempTxInfo);
}
/********************************************************************************************************************************************************************************************
**@function:		getATValidData	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
UINT16 getATValidData(char *p,UINT16 len,UINT16 offset)
{
	UINT16 i = 0;
	memset(p,'\0',len);
	printf("\r\n getATValidData: ");
	if((len + offset) < sizeof(gsmTaskInfo.tempRecData))
	{
		for(i = offset;i < len + offset;i++)
		{
			printf("%02x ",gsmTaskInfo.tempRecData[i]);
			p[i-offset] = gsmTaskInfo.tempRecData[i];
		}
		return len;
	}
	else
	{
		return 0;
	}
}
/********************************************************************************************************************************************************************************************
**@function:		getCharNum	
**@brief:	
**@param:
*************************************************************************************yt********************************************************************************************************/
void getCharNum(char *p,BYTE len)
{
    BYTE i = 0,k = 0;
    for(i = 0;i < gsmTaskInfo.tempRecDataLength;i++)
    {
        if(('0' <= gsmTaskInfo.tempRecData[i]) && ('9' >= gsmTaskInfo.tempRecData[i]))
        {
           p[k++] = gsmTaskInfo.tempRecData[i];
        }
    }
}
/********************************************************************************************************************************************************************************************
**@function:		debugRemainData	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void debugRemainData(void)
{
	UINT16 i = 0,len = 0;
	BYTE data = 0;
	
	len = FIFO_GET_COUNT(GSMRxFifo);
	if(len)
	{
		printf("\r\n len: %d: ",len);
		for(i = 0;i < len;i++)
		{
			FIFO_GET_ONE(GSMRxFifo,&data);
			printf("%c",data);
		}
	}
}
/********************************************************************************************************************************************************************************************
**@function:		getCompareStr	
**@brief:	
**@param:
*************************************************************************************yt********************************************************************************************************/
static BYTE getCompareStr(char *pSrc,UINT16 strLen)
{
	UINT16 i = 0;
	BYTE status = 0;
	//printf("\r\n getCompareStr: %s",pSrc);
	BYTE strcmpLength = 0;
	for(i = 0;i < strLen;i++)
	{
		switch(status)
		{
			case 0x00:		if(('\r' != pSrc[i]) && ('\n' != pSrc[i]))
							{
								memset(gsmTaskInfo.strcmpInfo[strcmpLength].cmpSrcStr,'\0',sizeof(T_STRCMP_INFO));
								gsmTaskInfo.strcmpInfo[strcmpLength].strlen = 0x00;
								status = 0x01;
								gsmTaskInfo.strcmpInfo[strcmpLength].cmpSrcStr[gsmTaskInfo.strcmpInfo[strcmpLength].strlen++] = pSrc[i];
								if((strLen - 1) == i)
								{
									strcmpLength = (strcmpLength + 1) % 5;
								}
							}
							break;
			
			case 0x01:		if('\r' == pSrc[i])
							{
								status = 0x02;
							}
							else
							{
								gsmTaskInfo.strcmpInfo[strcmpLength].cmpSrcStr[gsmTaskInfo.strcmpInfo[strcmpLength].strlen++] = pSrc[i];
								if((strLen - 1) == i)
								{
									strcmpLength = (strcmpLength + 1) % 5;
								}
							}
							break;
					
			case 0x02:		if('\n' == pSrc[i])
							{
								status = 0x00;
								printf("\r\n getCompareStr cmpSrcStr: %s",gsmTaskInfo.strcmpInfo[strcmpLength].cmpSrcStr);
								strcmpLength = (strcmpLength + 1) % 5;
								
							}
							break;
							
			default:		status = 0x00;
							break;
		}
	}
	
	//printf("\r\n getCompareStr strcmpLength: %d",strcmpLength);
	return strcmpLength;
}
/********************************************************************************************************************************************************************************************
**@function:		onCheckStrStr	
**@brief:	
**@param:
*************************************************************************************yt********************************************************************************************************/
static BYTE onCheckStrStr(char *pSrc,UINT16 strLen)
{
	UINT16 i = 0,len = 0,j = 0, k = 0,tt = 0;	
	BYTE rxData = 0x00;
	BYTE status = 0;
	BYTE minLen = 0;
	gsmTaskInfo.strcmpLength = getCompareStr(pSrc,strLen); 
	
	if(gsmTaskInfo.strcmpLength)
	{
		//printf("\r\n strcmpLength: %d",gsmTaskInfo.strcmpLength);
		minLen = gsmTaskInfo.strcmpInfo[0].strlen;
		for(i = 1;i < gsmTaskInfo.strcmpLength;i++)
		{
			if(minLen > gsmTaskInfo.strcmpInfo[i].strlen)
			{
				minLen = gsmTaskInfo.strcmpInfo[i].strlen;
			}
		}
		
		memset(gsmTaskInfo.cmpStr,'\0',sizeof(gsmTaskInfo.cmpStr));
		k = 0;
		memset(gsmTaskInfo.tempRecData,'\0',sizeof(gsmTaskInfo.tempRecData));
		gsmTaskInfo.tempRecDataLength = 0x00;
		
		len = FIFO_GET_COUNT(GSMRxFifo);
		//printf("\r\n 0:len: %d minLen: %d",len,minLen);
		if(len >= minLen)
		{	
			for(i = 0;i < minLen;i++)
			{
				FIFO_GET_ONE(GSMRxFifo,&rxData);
				gsmTaskInfo.cmpStr[k++] = (char)rxData;
				gsmTaskInfo.tempRecData[gsmTaskInfo.tempRecDataLength++] = rxData;
				//printf("[%c:%02x]",rxData,rxData);
			}
		
		}
		
		len = FIFO_GET_COUNT(GSMRxFifo);
		//printf("\r\n 1:len: %d",len);
		for(i = 0;i < len + 1;i++)
		{
			for(k = 0;k < gsmTaskInfo.strcmpLength;k++)
			{
				//printf("\r\n 0[%d]onCheckStrStr cmpStr: %s pSrc: %s ",k,gsmTaskInfo.cmpStr,gsmTaskInfo.strcmpInfo[k].cmpSrcStr);
				if(!memcmp(&gsmTaskInfo.strcmpInfo[k],gsmTaskInfo.cmpStr,minLen))
				{ 
					//printf("\r\n 1[%d]onCheckStrStr cmpStr: %s pSrc: %s ",k,gsmTaskInfo.cmpStr,gsmTaskInfo.strcmpInfo[k].cmpSrcStr);
					//printf("\r\n minLen: %d strlen: %d",minLen,gsmTaskInfo.strcmpInfo[k].strlen);
					if(minLen <= gsmTaskInfo.strcmpInfo[k].strlen)
					{
						//printf("\r\n len: %d",FIFO_GET_COUNT(GSMRxFifo));
						for(tt = minLen;tt < gsmTaskInfo.strcmpInfo[k].strlen;tt++)
						{
							FIFO_GET_ONE(GSMRxFifo,&rxData);
							//printf("[%c:%02x]",rxData,rxData);
							gsmTaskInfo.cmpStr[tt] = rxData;
						}
						//printf("\r\n len: %d",FIFO_GET_COUNT(GSMRxFifo));
						if(!memcmp(&gsmTaskInfo.strcmpInfo[k].cmpSrcStr,gsmTaskInfo.cmpStr,gsmTaskInfo.strcmpInfo[k].strlen))
						{
							printf("\r\n memcmp OK:%s",gsmTaskInfo.strcmpInfo[k].cmpSrcStr);
							return k + 1;	
						}	
					}
					else
					{
						printf("\r\n memcmp OK:%s",gsmTaskInfo.strcmpInfo[k].cmpSrcStr);
						return k + 1;	
					}
				}
			}
			
			FIFO_GET_ONE(GSMRxFifo,&rxData);
			//printf("[%c:%02x]",rxData,rxData);
			for(j = 1;j < minLen;j++)
			{
				gsmTaskInfo.cmpStr[j - 1] = gsmTaskInfo.cmpStr[j];
			}
			gsmTaskInfo.cmpStr[j - 1] = rxData;
			if(gsmTaskInfo.tempRecDataLength < sizeof(gsmTaskInfo.tempRecData))
			{
				gsmTaskInfo.tempRecData[gsmTaskInfo.tempRecDataLength++] = rxData;
			}
		}		
	}
	return 0x00;
}
/********************************************************************************************************************************************************************************************
**@function:		onGSMCommand	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
static BYTE onGSMCommand(BOOL bHex,char *pAtcmd,UINT16 atLen,char *pWaitChar,BYTE waitlen,UINT32 waitTime,BYTE *err)
{
	UINT16 tempLen = 0;
	BYTE res = 0x00;
	BYTE tempWaitLen = strlen(pWaitChar);
	switch(gsmTaskInfo.atCmdStatus)
	{
		case 0x00:	if((NULL != pAtcmd) && atLen)
					{	
						printATCmd(bHex,pAtcmd,atLen);
						ResetUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer);
						gsmTaskInfo.atCmdStatus = 0x01;
						*err = gsmWait;
					}
					else
					{
						gsmTaskInfo.atCmdStatus = 0x01;
						ResetUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer);
					}
					break;
		
		case 0x01:	if(ReadUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer) > 300)
					{
						ResetUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer);
						gsmTaskInfo.atCmdStatus = 0x02;
					}
					break;
		
		case 0x02:	tempLen = FIFO_GET_COUNT(GSMRxFifo);
					if(tempLen >= waitlen)
					{
						if(pWaitChar)
						{
							res = onCheckStrStr(pWaitChar,tempWaitLen);
							if(res)
							{
								*err = gsmOK;
								//printf("\r\n GSM REC: %s OK",pWaitChar);
								gsmTaskInfo.atCmdStatus = 0x00;	
								ResetUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer);
							}
						}
						else
						{
							*err = gsmOK;
						}
					}
					
					if(ReadUserTimer(&gsmTaskInfo.gsmATCmdDelayTimer) > waitTime)
					{
						gsmTaskInfo.atCmdStatus = 0x00;	
						*err = gsmTimeOut;						
					}
					break;
		
		default:	gsmTaskInfo.atCmdStatus = 0x00;
					break;
	}
	
	return res;
}
/********************************************************************************************************************************************************************************************
**@function:		onRecGSMFrameCmd	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void onRecGSMFrameCmd(char *p,UINT16 len)
{
	UINT16 i = 0;
	printf("\r\n 0.onRecGSMFrameCmd: ");
	for(i = 0;i < len;i++)
	{	
		printf("%c",p[i]);
	}
	
	gsmTaskInfo.onRecGSMFrameHexDataLength = 0;
	memset(gsmTaskInfo.onRecGSMFrameHexData,0,sizeof(gsmTaskInfo.onRecGSMFrameHexData));
	for(i = 0;i < len;i+=2)
	{	
		str2hex(&p[i],2,&gsmTaskInfo.onRecGSMFrameHexData[gsmTaskInfo.onRecGSMFrameHexDataLength++]);
	}
	
	printf("\r\n 1.onRecGSMFrameCmd: ");
	for(i = 0;i < gsmTaskInfo.onRecGSMFrameHexDataLength;i++)
	{	
		printf("%02x ",gsmTaskInfo.onRecGSMFrameHexData[i]);
	}
	ipcStartEvent(EVENT_GLOBAL_FRAME_DATA,0,gsmTaskInfo.onRecGSMFrameHexData,gsmTaskInfo.onRecGSMFrameHexDataLength);
}
/********************************************************************************************************************************************************************************************
**@function:		checkWhatSIMCardOperators	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
BYTE checkWhatSIMCardOperators(char *p,BYTE len)
{
	BYTE res = ChinaMobile;
	if(!memcmp(p,"46000",5))
	{
		res = ChinaMobile;
	}
	else if(!memcmp(p,"46002",5))
	{
		res = ChinaMobile;
	}
	else if(!memcmp(p,"46004",5))
	{
		res = ChinaMobile;
	}
	else if(!memcmp(p,"46007",5))
	{
		res = ChinaMobile;
	}
	else if(!memcmp(p,"46001",5))
	{
		res = ChinaUnicom;
	}
	else if(!memcmp(p,"46006",5))
	{
		res = ChinaUnicom;
	}
	else if(!memcmp(p,"46009",5))
	{
		res = ChinaUnicom;
	}
	
	return res;
}
/********************************************************************************************************************************************************************************************
**@function:		gsmPowerProc	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmPowerProc(void)
{
	UINT16 i = 0;
	BYTE error = 0;
	UINT16 tempLen = 0;
	BYTE rxData = 0x00;
	BYTE res = 0;
	T_GSM_TX_INFO tempTxInfo;
	switch(gsmTaskInfo.gsmPowerStatus)
	{
		case gsmProcInit:gsmTaskInfo.gsmPowerStatus = gsmStart;
						printf("\r\n GSM reset on.....");		
						break;
						
		case gsmStart:	GSM_PWR_OFF;
						GSM_RESET_ON;
						ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);
						gsmTaskInfo.gsmPowerStatus = gsmWaitStart0;										
						break;
											
		case gsmWaitStart0:if(ReadUserTimer(&gsmTaskInfo.gsmPowerDelayTimer) > T_1S)
						{
							GSM_PWR_ON;
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);
							gsmTaskInfo.gsmPowerStatus = gsmWaitStart1;							
						}	
						break;
						
		case gsmWaitStart1:if(ReadUserTimer(&gsmTaskInfo.gsmPowerDelayTimer) > T_1MS * 100)
						{
							GSM_RESET_OFF;
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);
							gsmTaskInfo.gsmPowerStatus = gsmATECHO;		
							ipcStartEvent(EVENT_GLOBAL_FRAME_GSM_CONNECT,FALSE,NULL,0);
						}	
						break;
													
		case gsmAT:		onGSMCommand(FALSE,AT_AT,strlen(AT_AT),"OK",2,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATECHO;
							FIFO_CLEAR(GSMRxFifo);
							//ipcStartEvent(EVENT_GLOBAL_AUTO_TBOX_CONNECT,FALSE,NULL,0);			
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmAT;
							//ipcStartEvent(EVENT_GLOBAL_AUTO_TBOX_CONNECT,FALSE,NULL,0);			
						}				
						break;
							
		case gsmATECHO:	onGSMCommand(FALSE,AT_ECHO,strlen(AT_ECHO),"OK",2,1000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCFUNC0;
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATECHO;
						}			
						break;
						
						
		case gsmATCFUNC0:onGSMCommand(FALSE,AT_CFUN0,strlen(AT_CFUN0),"OK",2,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATIMEI;
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCFUNC0;
						}	
						break;
										
		case gsmATIMEI:	onGSMCommand(FALSE,AT_IMEI,strlen(AT_IMEI),"OK",17,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCDP;
							memset(gsmTaskInfo.IMEI,'\0',16);                      
                            getCharNum(gsmTaskInfo.IMEI,16);
							printf("\r\n IMEI: %s",gsmTaskInfo.IMEI);
							ipcStartEvent(EVENT_GLOBAL_FRAME_GSM_IMEI,0,(BYTE *)gsmTaskInfo.IMEI,15);
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmStart;
						}					
						break;
												
		case gsmATNCDP:	onGSMCommand(FALSE,AT_NCDP,strlen(AT_NCDP),"OK",2,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{		
							gsmTaskInfo.gsmPowerStatus = gsmATCFUNC1;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCDP;
							printf("\r\n IMSI: %s",gsmTaskInfo.tempRecData);
						}
						break;
						
		case gsmATCFUNC1:onGSMCommand(FALSE,AT_CFUN1,strlen(AT_CFUN1),"OK",2,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCCID;
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCFUNC1;
						}	
						break;				
						
		case gsmATNCCID:onGSMCommand(FALSE,AT_NCCID,strlen(AT_NCCID),"OK",21,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCIMI;
							memset(gsmTaskInfo.ICCID,'\0',21);                      
                            getCharNum(gsmTaskInfo.ICCID,20);							
							printf("\r\n ICCID: %s",gsmTaskInfo.ICCID);
							ipcStartEvent(EVENT_GLOBAL_FRAME_GSM_ICCID,0,(BYTE *)gsmTaskInfo.ICCID,20);							
							FIFO_CLEAR(GSMRxFifo);							
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCCID;
						}
						break;
						
		case gsmATCIMI:	onGSMCommand(FALSE,AT_CIMI,strlen(AT_CIMI),"OK",17,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{
							memset(gsmTaskInfo.IMSI,'\0',16);    
							getCharNum(gsmTaskInfo.IMSI,16);							
							printf("\r\n IMSI: %s",gsmTaskInfo.IMSI);		
							gsmTaskInfo.gsmPowerStatus = gsmATBAND;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCIMI;
							printf("\r\n IMSI: %s",gsmTaskInfo.tempRecData);
						}
						break;
						
		case gsmATBAND:	onGSMCommand(FALSE,AT_NBAND,strlen(AT_NBAND),"OK",2,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCONFIG;
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATBAND;
						}	
						break;
						
		case gsmATNCONFIG:onGSMCommand(FALSE,AT_NCONFIG,strlen(AT_NCONFIG),"+NCONFIG",20,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGDCONT0;
							debugRemainData();
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNCONFIG;
						}	
						break;
						
		case gsmATCGDCONT0:onGSMCommand(FALSE,AT_CGDCONT0,strlen(AT_CGDCONT0),"OK",2,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGDCONT1;
							debugRemainData();
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGDCONT0;
						}	
						break;
		
		case gsmATCGDCONT1:onGSMCommand(FALSE,AT_CGDCONT1,strlen(AT_CGDCONT1),"+CGDCONT:",15,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGATT0;
							debugRemainData();
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGDCONT1;
						}	
						break;				
									
						
		case gsmATCGATT0:onGSMCommand(FALSE,AT_CGATT0,strlen(AT_CGATT0),"OK",2,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATCGATT1;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGATT0;
						}
						break;
		
		case gsmATCGATT1:onGSMCommand(FALSE,AT_CGATT1,strlen(AT_CGATT1),"+CGATT:1",8,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATCREG0;										
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;	
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGATT0;
						}
						break;
										
		case gsmATCREG0:onGSMCommand(FALSE,AT_CREG0,strlen(AT_CREG0),"OK",2,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATCREG1;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCREG0;
						}
						break;
		
						
		case gsmATCREG1:onGSMCommand(FALSE,AT_CREG1,strlen(AT_CREG1),"+CEREG:1,1\r\n+CEREG:1,5\r\n",10,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATCSCON;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCREG1;
						}
						break;	

		case gsmATCSCON:onGSMCommand(FALSE,AT_CSCON,strlen(AT_CSCON),"+CSCON:0,1",10,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATCGPADDR;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;	
							//debugRemainData();	
							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCSCON;
						}
						break;
						
		case gsmATCGPADDR:onGSMCommand(FALSE,AT_CGPADDR,strlen(AT_CGPADDR),"+CGPADDR:",20,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCCLK;
							debugRemainData();
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCGPADDR;
						}	
						break;	

		case gsmATCCLK:	onGSMCommand(FALSE,AT_CCLK,strlen(AT_CGPADDR),"+CCLK:",20,3000,&error);
						if(gsmOK == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATNNMI;
							gsmSetTimestamp();
							FIFO_CLEAR(GSMRxFifo);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCCLK;
						}	
						break;//
						
		case gsmATNNMI:	onGSMCommand(FALSE,AT_NNMI,strlen(AT_NNMI),"OK",2,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATNSMI;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCREG0;
						}
						break;
		
		case gsmATNSMI:	onGSMCommand(FALSE,AT_NSMI,strlen(AT_NSMI),"OK\r\n+NSMI:SENT\r\n",6,3000,&error);//onGSMCommand(FALSE,AT_CCID,strlen(AT_CCID),"OK",20,3000,&error);
						if(gsmOK == error)
						{							
							gsmTaskInfo.gsmPowerStatus = gsmATFreeLoop;						
							gsmTaskInfo.gsmConnectErrorCnt = 0x00;							
							FIFO_CLEAR(GSMRxFifo);
							ipcStartEvent(EVENT_GLOBAL_FRAME_GSM_CONNECT,TRUE,NULL,0);
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATCREG0;
						}
						break;		
									
		case gsmATFreeLoop:onGSMCommand(FALSE,NULL,0,"+NNMI:",8,3000,&error); //
						if(gsmOK == error)
						{	
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);	
							gsmTaskInfo.gsmPowerStatus = gsmATRD0;
							gsmTaskInfo.gsmNBDataLength = 0;
						}
						else if(FIFO_GET_COUNT(GSMTxFifo))
						{
							gsmTaskInfo.gsmPowerStatus = gsmATWD0;
							gsmTaskInfo.atCmdStatus  = 0X00;
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATFreeLoop;
//							printf("\r\n loop timeout: ");
							debugRemainData();
						}
						break;
						
		case gsmATWD0:	memset(AT_CMD,'\0',sizeof(AT_CMD));
						FIFO_GET_ONE(GSMTxFifo,&tempTxInfo);
						printf("\r\n frameLength: %d",tempTxInfo.frameLength);
						sprintf(AT_CMD,"AT+NMGS=%d,",tempTxInfo.frameLength);
						atcmdLength = strlen(AT_CMD);
						atcmdLength += hex2str(tempTxInfo.frameBuf,tempTxInfo.frameLength,&AT_CMD[atcmdLength]);
						
						AT_CMD[atcmdLength++] = '\r';
						AT_CMD[atcmdLength++] = '\n';
						gsmTaskInfo.gsmPowerStatus = gsmATWD1;
						break;
				
		case gsmATWD1:	res = onGSMCommand(FALSE,AT_CMD,atcmdLength,"OK",2,5000,&error);
						if(gsmOK == error)
						{
							printf("\r\n WD0 res: %d",res);
							gsmTaskInfo.gsmPowerStatus = gsmATFreeLoop;
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);	
						}
						else if(gsmTimeOut == error)
						{
							gsmTaskInfo.gsmPowerStatus = gsmATWD1;
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);
							printf("\r\n ATWD1 timeout...");
							//printf("\r\n WD:%s",gsmTaskInfo.tempRecData);
						}
						break;
						
		case gsmATRD0:	if(FIFO_GET_COUNT(GSMRxFifo) || (ReadUserTimer(&gsmTaskInfo.gsmPowerDelayTimer) > T_1S * 15))
						{
							FIFO_GET_ONE(GSMRxFifo,&rxData);
							if(',' == rxData)
							{
								gsmTaskInfo.gsmPowerStatus = gsmATRD1;	
								ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);	
								printf("\r\n gsmNBDataLength: %d",gsmTaskInfo.gsmNBDataLength);
								i = FIFO_GET_COUNT(GSMRxFifo);
								break;
							}
							else
							{
								if(('0' <= rxData) && ('9' >= rxData))
								{
									gsmTaskInfo.gsmNBDataLength *= 10;
									gsmTaskInfo.gsmNBDataLength += rxData - '0';								
								}					
							}
						}
						break;
						
		case gsmATRD1:	if((FIFO_GET_COUNT(GSMRxFifo) >= (gsmTaskInfo.gsmNBDataLength * 2)) || (ReadUserTimer(&gsmTaskInfo.gsmPowerDelayTimer) > T_1S * 5))
						{
							gsmTaskInfo.onRecGSMFrameChDataLength = 0;
							tempLen = FIFO_GET_COUNT(GSMRxFifo);
							printf("\r\n gsmATRD len: %d gsmNBDataLength:%d",tempLen,gsmTaskInfo.gsmNBDataLength);
							if(tempLen >= gsmTaskInfo.gsmNBDataLength * 2)
							{
								for(i = 0;i < gsmTaskInfo.gsmNBDataLength * 2;i++)
								{
									FIFO_GET_ONE(GSMRxFifo,&rxData);
									gsmTaskInfo.onRecGSMFrameChData[gsmTaskInfo.onRecGSMFrameChDataLength++] = rxData;
								}
								onRecGSMFrameCmd(gsmTaskInfo.onRecGSMFrameChData,gsmTaskInfo.onRecGSMFrameChDataLength);
							}
							ResetUserTimer(&gsmTaskInfo.gsmPowerDelayTimer);
							gsmTaskInfo.gsmPowerStatus = gsmATFreeLoop;			
						}
						break;
		
		
	
		default:		gsmTaskInfo.gsmPowerStatus = gsmStart;
						break;
	}
}
/********************************************************************************************************************************************************************************************
**@function:		ipcEventGsmComm	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void ipcEventGsmComm(ULONG enumWhatEvent,ULONG lPara,BYTE *p,UINT16 length)
{
	BOOL bNeedClear = TRUE;
	switch (enumWhatEvent)
	{
		case EVENT_GLOBAL_AUTO_CLR_INIT:	
											break;
		
		case EVENT_GLOBAL_GSM_FRAME:		printFrameToGSM(p,length);
											break;
		
		default:							
											bNeedClear = FALSE;
											break;
	}
	
	
	if (bNeedClear)
	{
		ipcClearEvent(enumWhatEvent);
	}
}
/********************************************************************************************************************************************************************************************
**@function:		gsmDebugProc	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmDebugProc(void)
{
#if 1	
	if(ReadUserTimer(&gsmTaskInfo.debugDelayTimer) > T_1S * 15)
	{
		ResetUserTimer(&gsmTaskInfo.debugDelayTimer);
		printf("\r\n =====================================");
		printf("\r\n gsmPowerStatus: %d",gsmTaskInfo.gsmPowerStatus);
		printf("\r\n bAllGSMSleep: %d",gsmTaskInfo.bAllGSMSleep);
		printf("\r\n =====================================");
	}
#endif	
}
/********************************************************************************************************************************************************************************************
**@function:		gsmTask	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmProc(void)
{
	UINT16 i = 0,len = 0;
	BYTE data = 0;
#if 1	
	gsmPowerProc();		
	gsmDebugProc();
#endif	
}
/********************************************************************************************************************************************************************************************
**@function:		gsmTaskDeInit	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmBoardDeInit(void)
{
	
}
/********************************************************************************************************************************************************************************************
**@function:		gsmTaskInit	
**@brief:	
**@param:
*********************************************************************************************************************************************************************************************/
void gsmBoardInit(void)
{
	memset(&gsmTaskInfo,0,sizeof(gsmTaskInfo));
}
